package com.szh.mysobproject.viewmodel.fishpond

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.szh.mysobproject.model.Fish
import com.szh.mysobproject.model.FishPondComment
import com.szh.mysobproject.paging.source.FishDetailCommendListPagingSource
import com.szh.mysobproject.paging.source.FishPagingSource
import com.szh.mysobproject.paging.source.UserFishPagingSource
import com.szh.mysobproject.viewmodel.app.Repository
import kotlinx.coroutines.flow.Flow

/**
 * @author : create by  szh
 * @date : 2022/6/24 14:53
 * @desc :摸鱼列表的 ViewModel
 */

class FishPondViewModel : ViewModel() {

    /**
     * 获取摸鱼标签
     */
    fun loadTopicList() = Repository.loadTopicList()

    /**
     * 获取动态评论
     */
    fun getFishCommendListById(momentId: String): Flow<PagingData<FishPondComment.FishPondCommentItem>> {
        return Pager(
            config = PagingConfig(30),
            pagingSourceFactory = {
                FishDetailCommendListPagingSource(momentId)
            }).flow.cachedIn(viewModelScope)
    }

    /**
     * 获取动态详情
     */
    fun getFishDetailById(momentId: String) = Repository.loadFishDetailById(momentId)

    /**
     * 点赞
     */
    fun dynamicLikes(momentId: String) = Repository.dynamicLikes(momentId)

    /**
     * 获取推荐动态
     */
    fun getFishListByCategoryId(topicId: String): Flow<PagingData<Fish.FishItem>> {
        return Pager(config = PagingConfig(30),
            pagingSourceFactory = {
                FishPagingSource(topicId)
            }).flow.cachedIn(viewModelScope)
    }

    /**
     * 评论
     */
    fun postComment(momentComment: Map<String, Any?>, isReply: Boolean) =
        Repository.postComment(momentComment, isReply)

    /**
     * 获取用户动态
     */
    fun getUserFishList(userId: String): Flow<PagingData<Fish.FishItem>> {
        return Pager(config = PagingConfig(30),
            pagingSourceFactory = {
                UserFishPagingSource(userId)
            }).flow.cachedIn(viewModelScope)

    }

    /**
     * 发布动态
     */
    fun putFish(moment: Map<String, Any?>) = Repository.putFish(moment)
}